package cn.xunjie.basic.controller;

import cn.xunjie.basic.dto.RegisterDto;
import cn.xunjie.basic.exception.BusinessException;
import cn.xunjie.basic.service.IVerifyCodeService;
import cn.xunjie.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    /*"/verifyCode/image/"+imageCodeKey*/
    @Autowired
    private IVerifyCodeService verifyCodeService;


   /* phone:"18925673384",
    imageCodeValue:"",
    imageCodeKey:""*/
   /* /verifycode/smsCode*/



    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String key){
        try {
            String base64Str = verifyCodeService.imageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

}
